package com.aitaoxue.palx.db;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.aitaoxue.palx.bean.UserInfos;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;

public class UserInfosDAO extends AbstractDao<UserInfos, Long> {

	public static final String TABLENAME = "USER_INFOS";

	public static class Properties {
		public final static Property Id = new Property(0, Long.class, "id",
				true, "_id");
		public final static Property Userid = new Property(1, String.class,
				"userid", false, "USERID");
		public final static Property Username = new Property(2, String.class,
				"username", false, "USERNAME");
		public final static Property Portrait = new Property(3, String.class,
				"portrait", false, "PORTRAIT");
	}

	public UserInfosDAO(DaoConfig config) {
		super(config);
	}

	public UserInfosDAO(DaoConfig config, RongyunDaoSession daoSession) {
		super(config, daoSession);
	}

	public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
		String constraint = ifNotExists ? "IF NOT EXISTS " : "";
		db.execSQL("create table " + constraint + "'USER_INFOS' ("
				+ "'_id' integer primary key,"
				+ "'USERID' text not null unique,"
				+ "'USERNAME' text not null," + "'PORTRAIT' text);");
	}

	public static void dropTable(SQLiteDatabase db, boolean ifExists) {
		String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "")
				+ "'USER_INFOS'";
		db.execSQL(sql);
	}

	@Override
	protected void bindValues(SQLiteStatement stmt, UserInfos entity) {
		stmt.clearBindings();

		Long id = entity.getId();
		if (id != null) {
			stmt.bindLong(1, id);
		}

		stmt.bindString(2, entity.getUserid());
		stmt.bindString(3, entity.getUsername());
		stmt.bindString(4, entity.getPortrait());
	}

	@Override
	protected Long getKey(UserInfos entity) {
		if (entity != null) {
			return entity.getId();
		} else {
			return null;
		}
	}

	@Override
	protected boolean isEntityUpdateable() {
		return true;
	}

	@Override
	protected UserInfos readEntity(Cursor cursor, int offset) {
		UserInfos entity = new UserInfos(cursor.isNull(offset + 0) ? null
				: cursor.getLong(offset + 0), cursor.getString(offset + 1),
				cursor.getString(offset + 2), cursor.getString(offset + 3));

		return entity;
	}

	@Override
	protected void readEntity(Cursor cursor, UserInfos entity, int offset) {
		entity.setId(cursor.isNull(offset + 0) ? null : cursor
				.getLong(offset + 0));
		entity.setUserid(cursor.getString(offset + 1));
		entity.setUsername(cursor.getString(offset + 2));
		entity.setPortrait(cursor.getString(offset + 3));
	}

	@Override
	protected Long readKey(Cursor cursor, int offset) {
		return cursor.isNull(offset) ? null : cursor.getLong(offset);
	}

	@Override
	protected Long updateKeyAfterInsert(UserInfos entity, long rowId) {
		entity.setId(rowId);
		return rowId;
	}

}
